package com.wang.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.commonutils.JwtUtils;
import com.wang.commonutils.MD5;
import com.wang.entity.UcenterMember;
import com.wang.entity.Vo.RegisterVo;
import com.wang.mapper.UcenterMemberMapper;
import com.wang.service.UcenterMemberService;
import com.wang.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.executor.statement.StatementUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-01-01
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 登录
     * @param member
     * @return
     */
    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        //判断手机号是否为空
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        //判断查询对象是否为空
        if (ucenterMember==null){
            //没有这个手机号
            throw new GuliException(20001,"登录失败");
        }
        //判断密码
        //把输入密码进行加密，再和数据库密码进行比较
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"登录失败");
        }
        //判断用户是否禁用
        if (ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败");
        }

        //登录成功
        //使用jwt工具类生成token字符串
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());
        return jwtToken;
    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //非空判断
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)||StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"为空");
        }

        //判断验证码
        //获取redis验证码
        String rediscode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(rediscode)){
            throw new GuliException(20001,"验证码错误");
        }
        //判断手机号是否重复，表里面存在相同的手机则不进行添加
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count>0){
            throw new GuliException(20001,"手机号码重复");
        }
        //添加入数据库
        UcenterMember ucenterMember = new UcenterMember();//构造对象

        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setAvatar("");
        ucenterMember.setIsDisabled(false);
        ucenterMember.setIsDeleted(false);
        ucenterMember.setNickname(nickname);

        baseMapper.insert(ucenterMember);

    }

    /**
     * 根据openid判断
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getOneIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember  member = baseMapper.selectOne(wrapper);
        return member;
    }

    /**
     * 查询某一天注册人数
     * @param day
     * @return
     */
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
